package com.exwave.socialgame.model_society.controller;

import com.exwave.socialgame.jwt_token.Token;
import com.exwave.socialgame.model_society.service.ResponseService;
import com.exwave.socialgame.model_wolfgame.wolfdata.Room;
import com.exwave.socialgame.model_society.entity.Response;
import com.exwave.socialgame.model_user.entity.User;
import com.exwave.socialgame.myutils.WolfGameUtils;
import com.exwave.socialgame.myutils.ParamsException;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * (Response)表控制层
 *
 * @author makejava
 * @since 2021-12-14 18:52:14
 */
@RestController
@RequestMapping("response")
public class ResponseController {
    /**
     * 服务对象
     */
    @Resource
    private ResponseService responseService;

    /**
     * 响应事件
     *
     * @param response
     */
    @Token.UserLoginToken
    @ApiOperation("响应组队")
    @PostMapping("addResponser")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token", required = true, dataTypeClass = String.class, paramType = "header"),
            @ApiImplicitParam(name = "uid", value = "响应者UID", required = true, dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "cid", value = "响应事件CID", required = true, dataTypeClass = Integer.class, paramType = "query")
    })
    public Object addResponser(Response response) {
        try {
            this.responseService.insert(response);
        } catch (ParamsException p) {
            return WolfGameUtils.returnFailure(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return WolfGameUtils.returnFailure(e.getMessage());
        }
        return WolfGameUtils.returnSuccess();
    }

    @Token.UserLoginToken
    @ApiOperation("取消响应")
    @PostMapping("delResponser")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token", required = true, dataTypeClass = String.class, paramType = "header"),
            @ApiImplicitParam(name = "uid", value = "响应者UID", required = true, dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "cid", value = "响应事件CID", required = true, dataTypeClass = Integer.class, paramType = "query")
    })
    public Object delResponser(@ApiIgnore Response response) {
        try {
            this.responseService.delete(response.getUid(), response.getCid());
        } catch (ParamsException p) {
            return WolfGameUtils.returnFailure(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return WolfGameUtils.returnFailure(e.getMessage());
        }
        return WolfGameUtils.returnSuccess();
    }


    @Token.UserLoginToken
    @ApiOperation("获取某帖子的响应者")
    @PostMapping("getResponser")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token", required = false, dataTypeClass = String.class, paramType = "header"),
            @ApiImplicitParam(name = "cid", value = "帖子CID", required = true, dataTypeClass = Integer.class, paramType = "query")
    })
    public Object getResponser(int cid) {
        List<Integer> uidList;
        HashMap<String, Object> map = new HashMap<>();
        try {
            uidList = this.responseService.queryAllByCid(cid);
            map.put("uidList", uidList);
        } catch (ParamsException p) {
            return WolfGameUtils.returnFailure(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return WolfGameUtils.returnFailure(e.getMessage());
        }
        return WolfGameUtils.returnSuccess(map);
    }

    @Token.UserLoginToken
    @ApiOperation("获取用户的所有响应帖子")
    @PostMapping("getCityteamer")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token", required = false, dataTypeClass = String.class, paramType = "header"),
            @ApiImplicitParam(name = "uid", value = "用户UID", required = true, dataTypeClass = Integer.class, paramType = "query")
    })
    public Object getCityteamer(int uid) {
        List<Integer> cidList;
        HashMap<String, Object> map = new HashMap<>();
        try {
            cidList = this.responseService.queryAllByUid(uid);
            map.put("cidList", cidList);
        } catch (ParamsException p) {
            return WolfGameUtils.returnFailure(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return WolfGameUtils.returnFailure(e.getMessage());
        }
        return WolfGameUtils.returnSuccess(map);
    }

    @ApiOperation("测试(反正也请求不成功)")
    @PostMapping("test")
    public void test(@RequestBody Response response,
                     @RequestBody User user,
                     @RequestBody Room room) {
        return;
    }
}